﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Runtime.Serialization;
namespace K.EnterpriseLibary
{
    [DataContract]
    [Serializable]
    public class AddonInfo : IdObject
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Category { get; set; }
        [DataMember]
        public string Description { get; set; }

        public string TypeFullName
        {
            get
            {
                if (Type == null)
                    return null;

                return Type.FullName;
            }
        }

        [DataMember]
        public string TypeAssemblyQualifiedName
        {
            get
            {
                if (Type == null)
                    return null;

                return Type.AssemblyQualifiedName;
            }
            set
            {
                if (value == null)
                    Type = null;
                else
                {
                    Type = Type.GetType(value, false);
                    if (Type == null)
                    {
                        throw new KException("Type is not loaded."
                            , "AssemblyQualifiedName", value);
                    }
                }
            }
        }

        public Type _Type;
        public Type Type
        {
            get
            {
                return _Type;
            }

            set
            {
                if (value != null)
                {
                    System.Resources.ResourceManager resourceManager = new System.Resources.ResourceManager(value);
                    if (resourceManager != null)
                    {
                        ID = new Guid(resourceManager.GetString(AddonPublic.AddonIDResourceName));
                        Name = resourceManager.GetString(AddonPublic.AddonNameResourceName);
                        Category = resourceManager.GetString(AddonPublic.AddonCategoryResourceName);
                        Description = resourceManager.GetString(AddonPublic.AddonDescriptionResourceName);
                    }
                    else
                    {
                        ID = Guid.Empty;
                        Name = null;
                        Category = null;
                        Description = null;
                    }
                    _Type = value;
                }
                TypeLoaded();
            }
        }

        public AddonInfo()
        {

        }

        public AddonInfo(Type addonType)
        {
            Type = addonType;
        }

        public bool IsEmpty
        {
            get
            {
                return (ID == Guid.Empty);
            }
        }

        protected internal virtual void TypeLoaded()
        {
        }

        public override string ToString()
        {
            return Name;
        }
    }
}
